<?php

namespace App\Admin\Controllers;

use \App\Models\QdcsClass;

use com_jjcbs\lib\ServiceFactory;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use ext\dao\ClassDaoImpl;
use ext\service\ClassServiceImpl;

class ClassController extends Controller
{
    use ModelForm;

    private $classDao = null;
    private $classService = null;

    public function __construct()
    {
        $this->classDao = new ClassDaoImpl();
        $this->classService = ServiceFactory::getInstance(ClassServiceImpl::class);
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('分类管理');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('分类管理');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('分类管理');
            $content->description('创建');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(QdcsClass::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->name('分类名');
            $grid->column('imgurl', '分类主图')->display(function ($cover) {
                return $cover ? "<img src='" . config('admin.upload.host') . $cover . "' width='50' height='50'>" : '<span>无</span>';
            });
            $grid->column('parent_id' , '上级分类')->display(function ($cover){
                return $cover == 0 ? '顶级分类' : ServiceFactory::getInstance(ClassServiceImpl::class)->getClassPath($cover);
            });
            $grid->sort('排序');
            $grid->is_delete('是否允许删除')->display(function($cover){
                return $cover ? '是' : '否';
            });
            $grid->created_at('创建时间');
            $grid->updated_at('编辑时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(QdcsClass::class, function (Form $form) {

            $form->display('id', '编号');
            $form->text('name' , '分类名称')->rules('required');
            $form->image('imgurl' , '分类主图');
            $form->select('parent_id' , '父级分类')->options($this->classService->getSelectOption())->default(0);
            $form->number('sort' , '排序值');
            $form->switch('is_delete' , '是否允许删除')->options([
                '是'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                '否' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ]);
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '编辑时间');
        });
    }
}
